package com.kyzh.core.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.gushenge.core.beans.HomeNews
import com.kyzh.core.R
import com.kyzh.core.uis.AutoScrollView
import java.util.*

class HomeNewsAdapter(val context: Context, val beans: MutableList<HomeNews>?, val height: Int) : BaseAdapter(), AutoScrollView.AutoScroll {
     override val immovableCount: Int
         get() = 1
     private val random = Random()
    private var mLayoutInflater: LayoutInflater? = null
    override fun getCount(): Int {
        if (beans != null) {
            return beans.size
        }
        return 0
    }

    override fun getItem(position: Int): Any? {
        return beans?.get(position)
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view:View
        val viewHolder: ViewHolder
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.frag_home_news_item1, parent, false)
            viewHolder = ViewHolder()
            viewHolder.tvTitle = view.findViewById<TextView>(R.id.text1)
            view.tag = viewHolder
        } else {
            view = convertView
            viewHolder = convertView.tag as ViewHolder
        }
        if (beans != null) {
            viewHolder.tvTitle?.text = if (beans.isEmpty()) {
                "当前暂无新闻"
            } else {
                beans?.get(position)?.name
            }
        }
        return view
    }

    /*传入item高度*/
    override fun getListItemHeight(context: Context): Int {
        return height
    }


    internal inner class ViewHolder {
        var tvTitle: TextView? = null
    }
}
